package com.jht.bletool2java.activity.setting;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.github.rahatarmanahmed.cpv.CircularProgressView;
import com.hjq.xtoast.OnClickListener;
import com.hjq.xtoast.XToast;

import com.jht.bletool2java.R;
import com.jht.bletool2java.activity.BaseActivity;
import com.jht.bletool2java.activity.setting.adapter.SettingAdapter;
import com.jht.bletool2java.activity.setting.bean.SettingItem;
import com.jht.bletool2java.bean.VersionChangeLog;
import com.jht.lib_common_ui.splash.SplashActivity;

import java.util.ArrayList;
import java.util.List;

public class SettingActivity extends BaseActivity implements ISetting.UI {
    private static final String TAG = "SettingActivity";
    private ISetting.Presenter settingPresenter;

    private RelativeLayout mCommonParent;
    private ImageView mIvBack;
    private ImageView mIvMenu;
    private TextView mTvTitle;
    private TextView mTvTitle2;
    private CircularProgressView mProgressView;

    private RecyclerView mRvSetting;
    private XToast setScanDialog;


    private SettingAdapter settingAdapter;

    @Override
    public int getLayoutID() {
        return R.layout.activity_setting;
    }

    @Override
    public void initViewAndPresenter() {
        settingPresenter = new SettingPresenter(this);
        mIvBack = findViewById(R.id.iv_back);
        mIvMenu = findViewById(R.id.iv_menu);
        mTvTitle = findViewById(R.id.tv_title);
        mTvTitle2 = findViewById(R.id.tv_title2);
        mProgressView = findViewById(R.id.progress_view);
        changeViewVisibility();
        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRvSetting = findViewById(R.id.rv_setting);
        initRecyclerView();

    }

    private void changeViewVisibility() {
        mIvBack.setVisibility(View.VISIBLE);
        mIvMenu.setVisibility(View.GONE);
        mTvTitle.setVisibility(View.GONE);
        mTvTitle2.setVisibility(View.VISIBLE);
        mCommonParent = findViewById(R.id.common_parent);
        mProgressView.setVisibility(View.GONE);
    }

    private void initRecyclerView() {
        View.OnClickListener onClickListener1 = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (setScanDialog == null) {
                    setScanDialog = new XToast(SettingActivity.this)
                            .setWindowFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                            .setDuration(0)
                            .setView(R.layout.dialog_set_scan_time)
                            .setAnimStyle(android.R.style.Animation_Dialog)
                            .setOnClickListener(R.id.tv_setting_dialog_ok, new OnClickListener() {
                                @Override
                                public void onClick(XToast toast, View view) {
                                    EditText editText = (EditText) toast.findViewById(R.id.et_time);
                                    Log.d(TAG, "onClick: editText => " + editText.getText().toString());
                                    String scanTime = editText.getText().toString();
                                    if ("".equals(scanTime)){
                                        toast.cancel();
                                        return;
                                    }
                                    try{
                                        int time = Integer.parseInt(scanTime);
                                        boolean result = settingPresenter.changeScanTime(time);
                                        editText.setText("");
                                        if (result){
                                            toast.cancel();
                                        }
                                    }catch (NumberFormatException e){
                                        editText.setText("");
                                        Toast.makeText(SettingActivity.this,getResources().getText(R.string.input_integer),Toast.LENGTH_SHORT).show();
                                    }
                                }
                            })
                            .setOnClickListener(R.id.ll_set_scan_time_dialog_parent, new OnClickListener() {
                                @Override
                                public void onClick(XToast toast, View view) {
                                    EditText editText = (EditText) toast.findViewById(R.id.et_time);
                                    editText.setText("");
                                    toast.cancel();
                                }
                            });
                }

                setScanDialog.show();
            }
        };
        View.OnClickListener onClickListener2 = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            }
        };
        View.OnClickListener onClickListener3 = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SettingActivity.this, SplashActivity.class);
                intent.putExtra("text_title", VersionChangeLog.titles);
                intent.putExtra("text_content",VersionChangeLog.contents);
                intent.putExtra("imgs",VersionChangeLog.imgs);
                startActivity(intent);
            }
        };
        List<View.OnClickListener> listenerArrayList = new ArrayList<>();
        listenerArrayList.add(onClickListener1);
        listenerArrayList.add(onClickListener2);
        listenerArrayList.add(onClickListener3);
        List<SettingItem> settingItemData = settingPresenter.getSettingItemData(listenerArrayList);
        settingAdapter = new SettingAdapter(this, settingItemData);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        mRvSetting.setLayoutManager(linearLayoutManager);
        mRvSetting.setAdapter(settingAdapter);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (setScanDialog != null) {
            setScanDialog.recycle();
        }

    }

    @Override
    public void changeScanTimeText(int time) {
        settingAdapter.changeScanTimeText(time);
    }

}